<!--  -->
<template>
  <div>
    <el-card class="main-card">
      <el-row>
        <el-col :span="8">
          <search :value="queryInfo.queryString" @search="querySearch"/>
        </el-col>
        <el-col :span="2">
          <el-button
            style="margin-left: 10px"
            v-hasPermi="['MENU_INSERT']"
            @click="insert"
            type="primary"
            >添加信息</el-button
          >
        </el-col>
      </el-row>
      <el-table
        :data="tableList"
        stripe
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        row-key="id"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <el-table-column prop="path" label="前端路由" />
        <el-table-column prop="icon" label="菜单图标" />
        <el-table-column prop="title" label="菜单标题" />
        <el-table-column prop="component" label="前端组件" />
        <el-table-column label="是否启用" v-hasPermi="['MENU_UPDATE']">
          <template slot-scope="scope">
            <el-switch
              @change="updateStatus(scope.row)"
              v-model="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="update(scope.row)"
              v-hasPermi="['MENU_UPDATE']"
              size="small"
              icon="el-icon-edit"
              circle
            />
            <el-button
              type="danger"
              @click="deleteById(scope.row.id)"
              v-hasPermi="['MENU_DELETE']"
              size="small"
              icon="el-icon-delete"
              circle
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="total > 0"
        @size-change="handlePageSize"
        @current-change="handlePageNumber"
        :current-page="queryInfo.pageNumber"
        :page-sizes="[7, 15, 50, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </el-card>
    <!-- 菜单对话框 -->
    <!-- 表单添加/修改 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="40%"
      @close="dialogClose"
    >
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        label-position="right"
        label-width="120px"
      >
        <el-form-item label="是否是父级菜单" v-if="isChildrenMenu">
          <el-checkbox v-model="isChildrenMenu" :disabled="disMenuSelect" />
        </el-form-item>
        <el-form-item label="父级菜单" prop="parentId" v-if="isChildrenMenu">
          <el-select
            v-model="form.parentId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in parentList"
              :key="index"
              :label="item.title"
              :value="item.id"
              :disabled="!item.status"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="前端路由" prop="path">
          <el-input v-model="form.path" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <e-icon-picker v-model="form.icon" />
        </el-form-item>
        <el-form-item label="菜单标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="前端组件" prop="component">
          <el-input v-model="form.component" />
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-radio-group v-model="form.status" class="status-class">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clickCancel">取 消</el-button>
        <el-button type="primary" @click="clickOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Search from "@/components/Search.vue";
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { Search },

  data() {
    //这里存放数据
    return {
      // 分页查询添加
      queryInfo: {
        // 页面
        pageNumber: 1,
        // 页数
        pageSize: 7,
        // 关键字
        queryString: null,
      },
      // 表格数据
      tableList: [],
      // 总记录数
      total: 0,
      // 表单标题
      title: null,
      // 是否打开对话框
      open: false,
      // 加载效果
      loading: false,
      // 表单数据
      form: {
        status: true,
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "请选择父级菜单", trigger: "change" },
        ],
        path: [
          { required: true, message: "请输入前端路由", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "前端路由的长度为1~100之间",
            trigger: "blur",
          },
        ],
        icon: [
          { required: true, message: "请选择菜单图标", trigger: "change" },
        ],
        title: [
          { required: true, message: "请输入菜单标题", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "菜单标题的长度为1~50之间",
            trigger: "blur",
          },
        ],
        component: [
          { required: true, message: "请输入菜单组件", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "菜单标题的长度为1~50之间",
            trigger: "blur",
          },
        ],
      },
      // 父级菜单列表
      parentList: [],
      // 是否是父级菜单
      isChildrenMenu: true,
      // 是否禁用下拉菜单
      disMenuSelect: false,
    };
  },

  //方法集合
  methods: {
    findParent() {
      this.$ajax.get("/menu/findParent").then((res) => {
        this.parentList = res.data;
      });
    },

    /* 分页查询 */
    findPage() {
      this.loading = true;
      this.$ajax.post("/menu/findPage", this.queryInfo).then((res) => {
        this.loading = false;
        this.tableList = res.rows;
        this.total = res.total;
      });
    },

    querySearch(value) {
      this.queryInfo.queryString = value;
      this.findPage();
    },

    /* 页码改变事件 */
    handlePageNumber(newPageNumber) {
      // 将点击页码后的新数据赋值给分页参数
      this.queryInfo.pageNumber = newPageNumber;
      // 调用分页方法，获取新页码对应的数据
      this.findPage();
    },

    /* 页面显示总条数改变事件 */
    handlePageSize(newPageSize) {
      // 将点击每页几条的新数据赋值给分页参数
      this.queryInfo.pageSize = newPageSize;
      // 调用分页方法，获取新的页面总条数
      this.findPage();
    },

    /* 改变菜单的状态 */
    updateStatus(row) {
      this.$ajax.put("/menu/update", row).then((res) => {
        this.$message.success("状态更新成功！");
      });
    },

    deleteById(id) {
      this.$confirm(
        "您的操作将永久删除编号为{" + id + "} 的数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 调用后端删除菜单接口
          this.$ajax.delete(`/menu/delete/${id}`).then((res) => {
            // 显示信息
            this.$message.success(res.message);
            // 跳转到第一页数据
            this.queryInfo.pageNumber = 1;
            // 重新查询数据
            this.findPage();
          });
        })
        .catch(() => {});
    },

    /* 点击新增 */
    insert() {
      //将整个表单进行重置，并移除校验效果
      this.form = {};
      this.form.status = true;
      this.title = "新增菜单";
      this.open = true;
    },

    /** 点击修改 */
    update(row) {
      this.title = "修改菜单";
      console.log(row);
      if (row.parentId === null) {
        // 隐藏父级菜单下拉列表
        this.isChildrenMenu = false;
      } else {
        // 禁用菜单级别选择
        this.disMenuSelect = true;
      }
      this.form = row;
      this.open = true;
    },

    /* 关闭对话框的事件 */
    dialogClose() {
      this.isChildrenMenu = true;
      this.disMenuSelect = false;
      //将整个表单进行重置，并移除校验效果
      this.$refs.form.resetFields();
      this.form = {};
      this.form.status = true;
      // 点击x号后需重置页面
      this.findPage();
    },

    /* 点击取消 */
    clickCancel() {
      //将整个表单进行重置，并移除校验效果
      this.$refs.form.resetFields();
      this.form = {};
      this.form.status = true;
      this.open = false;
      this.isChildrenMenu = true;
      this.disMenuSelect = false;
      // 由于将form变成了空对象，点击取消后需重置页面
      this.findPage();
    },

    /* 点击确定 */
    clickOk() {
      // 进行表单校验
      this.$refs.form.validate((valid) => {
        //校验不通过
        if (!valid)
          return this.$message.error("表单检验不通过，请检查后提交！");
        //校验通过 判断是否是新增(新增时不带id，修改时带id)
        if (this.form.id === undefined || this.form.id === null) {
          this.$ajax.post("/menu/insert", this.form).then((res) => {
            this.$message.success(res.message);
            this.open = false;
            this.findPage();
          });
        } else {
          this.$ajax.put("/menu/update", this.form).then((res) => {
            this.$message.success(res.message);
            this.open = false;
            this.findPage();
          });
        }
      });
    },
  },

  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.findPage();
    this.findParent();
  },
};
</script>
<style scoped>
.status-class {
  margin-left: -75%;
}
</style>
